#ifndef SERVER_H
#define SERVER_H

#include <iostream>
#include <sys/epoll.h>
#include <unistd.h>
#include <fcntl.h>
#include <cstring>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/eventfd.h>
#include <thread>
#include <chrono>
#include <mutex>
#include <map>
#include <functional>
#include "threadpool.h"
#include "Channel.h"
#include <unordered_map>
#include "Common.h"
#define MAX_EVENTS 10


class Channel;
class EventLoop
{
public:
    typedef std::shared_ptr<EventLoop> ptr;
    // 获取单例对象
    static std::shared_ptr<EventLoop> getInstance();

 

    void sendMessages(uint8_t idx, uint8_t cmd, uint8_t *data);
    
    void update_event(int operation, Channel *channel);
private:
    // 构造函数私有化，防止外部创建实例
    EventLoop();

    // 禁止拷贝和赋值
    EventLoop(const EventLoop &) = delete;
    EventLoop &operator=(const EventLoop &) = delete;

    // 服务器资源
    int epoll_fd;
    struct epoll_event events[MAX_EVENTS];

    std::thread clientHandlerThread;
    // 启动服务器
    void start();

    // 设置socket为非阻塞
    int setNonBlocking(int sockfd);

    // 处理客户端的线程
    void handleClient();

};

#endif // SERVER_H
